cd "VirtualBox-$surum"

#patch -p1 -N -i /sources/001-disable-update.patch
#patch -p1 -N -i /sources/virtualbox-host-dkms.conf
#patch -p1 -N -i /sources/virtualbox-vboxsf-dkms.conf
#patch -p1 -N -i /sources/vboxdrmclient.path
patch -p1 -N -i /sources/001-disable-update.patch
patch -p1 -N -i /sources/005-gsoap-build.patch
patch -p1 -N -i /sources/006-rdesktop-vrdp-keymap-path.patch
patch -p1 -N -i /sources/008-no-vboxvideo.patch
patch -p1 -N -i /sources/012-vbglR3GuestCtrlDetectPeekGetCancelSupport.patch
patch -p1 -N -i /sources/013-Makefile.patch
#patch -p1 -N -i /sources/016-VBoxServiceAutoMount-Change-Linux-mount-code-to-use-.patch
patch -p1 -N -i /sources/017-fix-narrowing-conversion.patch
patch -p1 -N -i /sources/018-xclient.patch
#patch -p1 -N -i /sources/019-python3.9.patch
#patch -p1 -N -i /sources/020-linux-5-11.patch


    echo 'Yerel ayarlar uygulanıyor'
    cp "/sources/LocalConfig.kmk" .

    echo 'Bizim CFLAGS uygulanıyor'
    echo "VBOX_GCC_OPT=$CXXFLAGS" >> LocalConfig.kmk


    ./configure \
        --disable-docs \
        --disable-kmods \
        --disable-vmmraw \
        --enable-vde \
        --enable-vnc \
        --enable-webservice \
        --with-makeself=/usr/bin/echo
    # nofatal olmadan derlemek için sahte makeelf ikili

    source ./env.sh
    kmk

    echo 'Build rdesktop-vrdp'
    kmk -C src/VBox/RDP/client-1.8.4

    echo 'VNC extension yama inşa ediliyor'
    kmk -C src/VBox/ExtPacks/VNC packing

    echo 'Build vboximg-mount'
    kmk -C src/VBox/ImageMounter/vboximg-mount

cd $SRC
    cd "VirtualBox-$surum/out/linux.$BUILD_PLATFORM_ARCH/release/bin"

    # binaries
    install -dm0755 "$PKG/usr/bin"
    install -m0755 VBox.sh "$PKG/usr/bin/VBox"
    for i in VBoxHeadless VBoxManage VBoxSDL VirtualBox vboxwebsrv VBoxBalloonCtrl; do
        ln -sf VBox "$PKG/usr/bin/$i"
        ln -sf VBox "$PKG/usr/bin/${i,,}"
    done
    install -m0755 VBoxTunctl "$PKG/usr/bin"
    install -m0755 rdesktop-vrdp "$PKG/usr/bin"
    install -m0755 vboximg-mount "$PKG/usr/bin"

    # libraries
    install -dm0755 "$PKG/usr/lib/virtualbox"
    install -m0755 *.so "$PKG/usr/lib/virtualbox"
    install -m0644 *.r0 VBoxEFI*.fd "$PKG/usr/lib/virtualbox"
    ## setuid root binaries
    install -m4755 VBoxSDL VirtualBoxVM VBoxHeadless VBoxNetDHCP VBoxNetAdpCtl VBoxNetNAT -t "$PKG/usr/lib/virtualbox"
    ## other binaries
    install -m0755 VirtualBox VBoxManage VBoxSVC VBoxExtPackHelperApp VBoxXPCOMIPCD VBoxTestOGL VBoxBalloonCtrl vboxwebsrv webtest -t "$PKG/usr/lib/virtualbox"

    # components
    install -dm0755 "$PKG/usr/lib/virtualbox/components"
    rm components/VBoxREM.so # TODO: remove when dead link is fixed
    install -m0755 components/* -t "$PKG/usr/lib/virtualbox/components"

    # extensions packs
    ## as virtualbox install itself stuff in this directory, move it to /var and
    ## trick it with a symlink
    ## FIXME: trick is disabled for now
    #install -dm0755 "$PKG/var/lib/virtualbox/extensions"
    #install -dm0755 "$PKG/usr/share/virtualbox/extensions"
    #ln -s ../../../var/lib/virtualbox/extensions "$PKG/usr/lib/virtualbox/ExtensionPacks"
    install -dm0755 "$PKG/usr/lib/virtualbox/ExtensionPacks"

    # languages
    install -dm0755 "$PKG/usr/share/virtualbox/nls"
    install -m0755 nls/*.qm -t "$PKG/usr/share/virtualbox/nls"

    # rdesktop keymaps
    install -dm0755 "$PKG/usr/share/virtualbox/rdesktop-vrdp-keymaps"
    install -m0644 rdesktop-vrdp-keymaps/* "$PKG/usr/share/virtualbox/rdesktop-vrdp-keymaps"

    # useless scripts
    install -m0755 VBoxCreateUSBNode.sh VBoxSysInfo.sh -t "$PKG/usr/share/virtualbox"

    # icons
    install -Dm0644 VBox.png "$PKG/usr/share/pixmaps/VBox.png"

    pushd icons >/dev/null
    for i in *; do
        install -d "$PKG/usr/share/icons/hicolor/$i/mimetypes"
        cp $i/* "$PKG/usr/share/icons/hicolor/$i/mimetypes"
    done
    popd >/dev/null

    #desktop
    install -Dm0644 virtualbox.desktop "$PKG/usr/share/applications/virtualbox.desktop"
    install -Dm0644 virtualbox.xml "$PKG/usr/share/mime/packages/virtualbox.xml"

    #install configuration
    install -dm0755 "$PKG/etc/vbox"
    echo 'INSTALL_DIR=/usr/lib/virtualbox' > "$PKG/etc/vbox/vbox.cfg"

    # back to srcdir
    cd "$SRC"


    # install systemd stuff
 #   install -Dm0644 60-vboxdrv.rules "$PKG/usr/lib/udev/rules.d/60-vboxdrv.rules"
 #   install -Dm0644 vboxweb.service "$PKG/usr/lib/systemd/system/vboxweb.service"
    install -Dm0644 virtualbox.sysusers "$PKG/usr/lib/sysusers.d/virtualbox.conf"

    # install module reloading shortcut (with a symlink with default helper)
    install -Dm0755 vboxreload "$PKG/usr/bin"
    ln -s vboxreload "$PKG/usr/bin/rcvboxdrv"

    cd "$SRC"

    install -dm0755 "$PKG/usr/lib/virtualbox"

    source "VirtualBox-$PKG/env.sh"
    cd "VirtualBox-$surum/out/linux.$BUILD_PLATFORM_ARCH/release/bin"

    install -Dm0755 vboxshell.py "$PKG/usr/lib/virtualbox/vboxshell.py"
    # python sdk
    pushd sdk/installer
    VBOX_INSTALL_PATH="/usr/lib/virtualbox" python vboxapisetup.py install --root "$PKG"
    popd
    cp -r sdk "$PKG/usr/lib/virtualbox"
    rm -r "$PKG/usr/lib/virtualbox/sdk/installer"
 
    cd "$SRC"


    install -dm0755 "$PKG/usr/src"
    source "VirtualBox-$surum/env.sh"
    cd "VirtualBox-$pkgver/out/linux.$BUILD_PLATFORM_ARCH/release/bin"
    cp -r src "$PKG/usr/src/vboxhost-${surum}_OSE"

    # module loading
    local _p="$PKG/usr/lib/modules-load.d/$isim.conf"
    install -Dm0644 /dev/null "$_p"
    printf "vboxdrv\nvboxnetadp\nvboxnetflt\n" > "$_p"
    # starting vbox 5.1, dkms.conf file was dropped
    local _p="$PKG/usr/src/vboxhost-${surum}_OSE/dkms.conf"
    install -Dm0644 "$SRC/$isim.conf" "$_p"
    sed -i "s,@VERSION@,$surum," "$_p"

   cd "$SRC"
    install -dm0755 "$PKG/usr/src"
    source "VirtualBox-$surum/env.sh"
    cd "VirtualBox-$surum/out/linux.$BUILD_PLATFORM_ARCH/release/bin/additions"


    # module loading
    local _p="$PKG/usr/lib/modules-load.d/$isim.conf"
    install -Dm0644 /dev/null "$_p"
    printf "vboxguest\nvboxsf\nvboxvideo\n" > "$_p"

    # vboxsf module for Linux 4.16 to Linux 5.5
    install -d "$pkgdir/usr/src/vboxsf-${surum}_OSE"
    cp -rT "$SRC/vboxsf" "$PKG/usr/src/vboxsf-${surum}_OSE/vboxsf"
    rm -rf "$PKG/usr/src/vboxsf-${surum}_OSE/vboxsf/.git"
    echo "obj-m = vboxsf/" >"$PKG/usr/src/vboxsf-${surum}_OSE/Makefile"
    local _p="$PKG/usr/src/vboxsf-${surum}_OSE/dkms.conf"
    install -Dm0644 "$SRC/virtualbox-vboxsf-dkms.conf" "$_p"
    sed -i "s,@VERSION@,$surum," "$_p"
cd $SRC
    source "VirtualBox-$surum/env.sh"
    pushd "VirtualBox-$surum/out/linux.$BUILD_PLATFORM_ARCH/release/bin/additions"
    install -d "$PKG/usr/bin"
    install -m0755 VBoxClient VBoxControl VBoxDRMClient VBoxService "$PKG/usr/bin"
    install -m0755 -D "$SRC"/VirtualBox-$surum/src/VBox/Additions/x11/Installer/98vboxadd-xclient \
        "$PKG"/usr/bin/VBoxClient-all
    install -m0644 -D "$srcdir"/VirtualBox-$surum/src/VBox/Additions/x11/Installer/vboxclient.desktop \
        "$PKG"/etc/xdg/autostart/vboxclient.desktop
    install -m0755 -D pam_vbox.so "$PKG/usr/lib/security/pam_vbox.so"
    popd
    # systemd stuff
    install -Dm0644 60-vboxguest.rules "$PKG/usr/lib/udev/rules.d/60-vboxguest.rules"
    install -Dm0644 vboxdrmclient.path "$PKG/usr/lib/systemd/system/vboxdrmclient.path"
  #  install -Dm0644 vboxdrmclient.service "$PKG/usr/lib/systemd/system/vboxdrmclient.service"
   # install -Dm0644 vboxservice.service "$PKG/usr/lib/systemd/system/vboxservice.service"
    install -Dm0644 virtualbox-guest-utils.sysusers "$PKG/usr/lib/sysusers.d/virtualbox-guest-utils.conf"

cd $SRC
    source "VirtualBox-$surum/env.sh"
    pushd "VirtualBox-$surum/out/linux.$BUILD_PLATFORM_ARCH/release/bin/additions"
    install -d "$PKG/usr/bin"
    install -m0755 VBoxControl VBoxService "$PKG/usr/bin"
    install -m0755 -D pam_vbox.so "$PKG/usr/lib/security/pam_vbox.so"
    popd
    # systemd stuff
    install -Dm0644 60-vboxguest.rules "$PKG/usr/lib/udev/rules.d/60-vboxguest.rules"
  #  install -Dm0644 vboxservice-nox.service "$PKG/usr/lib/systemd/system/vboxservice.service"
   # install -Dm0644 virtualbox-guest-utils.sysusers "$PKG/usr/lib/sysusers.d/virtualbox-guest-utils.conf"


cd $SRC
    source "VirtualBox-$surum/env.sh"
    cd "VirtualBox-$surum/out/linux.$BUILD_PLATFORM_ARCH/release/packages"
    install -Dm0644 VNC-*.vbox-extpack "$PKG/usr/share/virtualbox/extensions/VNC-${surum}.vbox-extpack"

